package org.jeecg.modules.uwms;


import cn.hutool.core.io.resource.ResourceUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;

import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.RedisUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Description: 测试demo 
 * @Author: jeecg-boot 
 * @Date:2018-12-29 
 * @Version:V1.0
 */
@Slf4j
@Api(tags="UWMS")
@RestController
@RequestMapping("/uwms/client")
public class UwmsClientController  {
	@Value("${jeecg.path.upload}")
	private String upLoadPath;

	@Autowired
	private RedisUtil redisUtil;




	/**
	 * 添加
	 *
	 * @param jeecgDemo
	 * @return
	 */
	@PostMapping(value = "/senddata")
	@AutoLog(value = "发送数据")
	@ApiOperation(value = "发送数据", notes = "发送数据")
	public Result<?> senddata(@RequestBody Uwmsdata jeecgDemo) {
		Result<Uwmsdata> result = new Result<Uwmsdata>();
		try {
			redisUtil.set(jeecgDemo.getData02(), jeecgDemo.getData01()+jeecgDemo.getData03()+jeecgDemo.getData04());
			result.success("发送数据成功！");
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			result.error500("操作失败");
		}
		return result;
	}
	/**
	 * 添加
	 *
	 * @return
	 */
	@GetMapping(value = "/getdata")
	@AutoLog(value = "get")
	@ApiOperation(value = "发送数据", notes = "发送数据")
	public Result<?> gettest(@RequestParam  String indata) {
		Result<Uwmsdata> result = new Result<Uwmsdata>();
		try {
			redisUtil.set(indata, indata);
			result.success("发送数据成功！");
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			result.error500("操作失败");
		}
		return result;
	}
	/**
	 * 添加
	 *
	 * @param jeecgDemo
	 * @return
	 */
	@PostMapping(value = "/receivedata")
	@AutoLog(value = "接收数据")
	@ApiOperation(value = "接收数据", notes = "接收数据")
	public Result<?> receivedata(@RequestBody Uwmsdata jeecgDemo) {
		Result<Uwmsdata> result = new Result<Uwmsdata>();
		try {
//			redisUtil.set(jeecgDemo.getData02(), jeecgDemo.getData01()+jeecgDemo.getData03()+jeecgDemo.getData04());
			result.success("发送数据成功！");
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			result.error500("操作失败");
		}
		return result;
	}
	@RequestMapping(value = "/print/bq/{username}", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@ApiOperation(value = "根据vbeln1生成合同", notes = "vbeln1", httpMethod = "POST", produces = "application/json")
	public Result<?> batchdownbq(@PathVariable("username") String username,
												@RequestBody printheader printheaderobj) {
		Result<Uwmsdata> result = new Result<Uwmsdata>();
		OutputStream fileOut = null;
		BufferedImage bufferImg = null;

		ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
		HSSFWorkbook wb = new HSSFWorkbook();
		CellStyle cs = wb.createCellStyle();
		CellStyle cs1 = wb.createCellStyle();
		CellStyle cs2 = wb.createCellStyle();

		// 创建两种字体
		Font f = wb.createFont();
		Font f1 = wb.createFont();
		Font f2 = wb.createFont();

		// 创建第一种字体样式（用于列名）
		f.setFontHeightInPoints((short) 12);
		f.setColor(IndexedColors.BLACK.getIndex());
//				f.setBoldweight(Font.BOLDWEIGHT_BOLD);
// 创建两种字体

		// 创建第一种字体样式（用于列名）
		f1.setFontHeightInPoints((short) 8);
		f1.setColor(IndexedColors.BLACK.getIndex());
//				f1.setBoldweight(Font.BOLDWEIGHT_BOLD);

		f2.setFontHeightInPoints((short) 10);
		f2.setColor(IndexedColors.BLACK.getIndex());
		// 设置第一种单元格的样式（用于列名）
		cs.setFont(f);
		cs.setBorderLeft(CellStyle.BORDER_NONE);
		cs.setBorderRight(CellStyle.BORDER_NONE);
		cs.setBorderTop(CellStyle.BORDER_NONE);
		cs.setBorderBottom(CellStyle.BORDER_NONE);
		cs.setAlignment(HSSFCellStyle.ALIGN_LEFT);
		cs1.setFont(f1);
		cs1.setBorderLeft(CellStyle.BORDER_THIN);
		cs1.setBorderRight(CellStyle.BORDER_THIN);
		cs1.setBorderTop(CellStyle.BORDER_THIN);
		cs1.setBorderBottom(CellStyle.BORDER_THIN);
		cs1.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		cs1.setWrapText(true);
		cs2.setFont(f2);
		cs2.setBorderLeft(CellStyle.BORDER_NONE);
		cs2.setBorderRight(CellStyle.BORDER_NONE);
		cs2.setBorderTop(CellStyle.BORDER_NONE);
		cs2.setBorderBottom(CellStyle.BORDER_NONE);
		cs2.setAlignment(HSSFCellStyle.ALIGN_LEFT);
		cs2.setWrapText(true);


		HSSFSheet sheet = wb.createSheet("打印");

		sheet.setMargin(HSSFSheet.TopMargin, 0.1);// 页边距（上）
		sheet.setMargin(HSSFSheet.BottomMargin, 0.1);// 页边距（下）
		sheet.setMargin(HSSFSheet.LeftMargin, 0.1);// 页边距（左）
		sheet.setMargin(HSSFSheet.RightMargin, 0.1);// 页边距（右
		sheet.setColumnWidth(0, 3 * 256);
		sheet.setColumnWidth(1, 25 * 256);
		sheet.setColumnWidth(2, 10 * 256);
		sheet.setColumnWidth(3, 13 * 200);

//			sheet.setColumnWidth(3, 14 * 256);
//			sheet.setColumnWidth(4, 14 * 256);
//			sheet.setColumnWidth(5, 6 * 256);
		HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
		HSSFClientAnchor anchor = null;

		int cellsNum = 0;
		int pagesize = 1;
		int page = 0;
		short high = 300;
		List<printitem> listitem = printheaderobj.getPrintitem();
					cellsNum++;
			Row rowColumnValue5 = sheet.createRow((short) page * pagesize + cellsNum); // 列名
			Cell cell6 = rowColumnValue5.createCell(0);
			cell6.setCellValue("");
			CellRangeAddress c6 = new CellRangeAddress(page * pagesize + cellsNum, page * pagesize + cellsNum, 0, 3);


		for (printitem t : listitem) {

			Row rowColumnValue = sheet.createRow((short) page * pagesize + cellsNum); // 列名
			rowColumnValue.setHeight((short) high);
			Cell cell1 = rowColumnValue.createCell(1);
			cell1.setCellValue(t.getData01());
			cell1.setCellStyle(cs1);
//			CellRangeAddress c0 = new CellRangeAddress(page * pagesize + cellsNum, page * pagesize + cellsNum, 0, 2);
//			sheet.addMergedRegion(c0);

//			cellsNum++;
//			Row rowColumnValue1 = sheet.createRow((short) page * pagesize + cellsNum); // 列名
			rowColumnValue.setHeight((short) high);
			Cell cell2 = rowColumnValue.createCell(2);
			cell2.setCellValue(t.getData02());
			cell2.setCellStyle(cs1);
//			CellRangeAddress c2 = new CellRangeAddress(page * pagesize + cellsNum, page * pagesize + cellsNum, 0, 2);
//			sheet.addMergedRegion(c2);
//			cellsNum++;

//			Row rowColumnValue12 = sheet.createRow((short) page * pagesize + cellsNum); // 列名
			rowColumnValue.setHeight((short) high);
			Cell cell13 = rowColumnValue.createCell(3);
			cell13.setCellValue(t.getData03());
			cell13.setCellStyle(cs1);
//			cell13.setCellStyle(cs1);
//			CellRangeAddress c13 = new CellRangeAddress(page * pagesize + cellsNum, page * pagesize + cellsNum, 0, 2);
//			sheet.addMergedRegion(c13);
			cellsNum++;
//
//			Row rowColumnValue2 = sheet.createRow((short) page * pagesize + cellsNum); // 列名
//			rowColumnValue2.setHeight(high);
//			Cell cell4 = rowColumnValue2.createCell(0);
//			cell4.setCellValue("");
//			cell4.setCellStyle(cs);
//			CellRangeAddress c3 = new CellRangeAddress(page * pagesize + cellsNum, page * pagesize + cellsNum, 0, 2);
//			sheet.addMergedRegion(c3);
//			cellsNum++;
//
//			Row rowColumnValue4 = sheet.createRow((short) page * pagesize + cellsNum); // 列名
//			rowColumnValue4.setHeight(high);
//			Cell cell5 = rowColumnValue4.createCell(0);
//			cell5.setCellValue("");
//			cell5.setCellStyle(cs);
//			CellRangeAddress c5 = new CellRangeAddress(page * pagesize + cellsNum, page * pagesize + cellsNum, 0, 2);
//			sheet.addMergedRegion(c5);
//
//			cellsNum++;
//			Row rowColumnValue5 = sheet.createRow((short) page * pagesize + cellsNum); // 列名
//			Cell cell6 = rowColumnValue5.createCell(0);
//			cell6.setCellValue("");
//			CellRangeAddress c6 = new CellRangeAddress(page * pagesize + cellsNum, page * pagesize + cellsNum, 0, 2);
//
//			page++;

		}

		try {
			// 第七步，将文件存到指定位置并打印
 			String filest = DateUtils.date2Str(DateUtils.yyyymmddhhmmss);
			String savaPath = upLoadPath;
			String fileurl = "wlbq" + username + "/" + filest + "/"+ filest + ".xls";

			File file = new File(savaPath + "/" + "wlbq" + username + "/" + filest);
			savaPath = savaPath.replace("D:/","D:/");

			if (!file.exists()) {
				file.mkdirs();// 创建文件根目录
			}
			String outfilePath = savaPath + "/" + fileurl;
			try {
				FileOutputStream fileOutputStream = new FileOutputStream(outfilePath);//指定路径与名字和格式
				wb.write(fileOutputStream);//将数据写出去
				fileOutputStream.close();//关闭输出流
				printUtil.printfile(outfilePath);
			} catch (Exception e) {
				e.printStackTrace();
			}
			System.out.println("----Excle文件已生成------");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (fileOut != null) {
				try {
					fileOut.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}

}
